package May;

import java.util.Random;
import java.util.Scanner;

//该项目完美完成
public class Test07Random {
    public static void main(String[] args) throws InterruptedException {
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请选择游戏模式，输入1为系统随机数模式，输入2为用户自定义模式（只能输入0~100范围内）");
            int temp = scanner.nextInt();
            if (temp == 1) {
                Sys();
                break;
            } else if (temp == 2) {
                user();
                break;
            } else {
                System.out.println("输入的数字错误！请重新输入");
            }
        }
    }

    public static void Sys() {
        //生成随机数
        Random ran = new Random();
        //设置随机数范围为0~100
        int result = ran.nextInt(100) + 1;
        int min = 0;
        int max = 100;
        //键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请猜想一个小于100的数字，我将告诉你是否为炸弹，如果不是，将缩小范围");
        System.out.println("后台生成的数为：" + result);
        //while循环方便处理非法数字
        while (true) {
            int number = sc.nextInt();
            if (number < min || number > max) {
                System.out.println("输入的数字超出范围，请重新输入");
                continue;
            }
            if (number == result) {
                System.out.println("游戏结束！这是炸弹");
                break;
            } else if (number > result) {
                if (min < number) {
                    max = number;
                    System.out.println("游戏继续，并且范围缩小为(" + min + "," + max + "),你现在只能输入该范围内的数字");
                }
            } else {
                if (max > number) {
                    min = number;
                    System.out.println("游戏继续，并且范围缩小为(" + min + "," + max + ")你现在只能输入该范围内的数字");
                }
            }
        }
    }

    public static void user() throws InterruptedException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字，我将其记录为‘炸弹’");
        int result = sc.nextInt();
        System.out.println("请开始游戏，下面请手动清除控制台输出的内容。");
        //等待清理控制台输出的内容，2.7s
        Thread.sleep(2700);
        System.out.println("清除成功，请开始猜一个数字");
        int min = 0;
        int max = 100;
        while (true) {
            int number = sc.nextInt();
            if (number < min || number > max) {
                System.out.println("您输入的数字范围不合法！请重新输入");
                continue;
            }
            if (number == result) {
                System.out.println("您的游戏结束，炸弹就是" + result);
                break;
            } else if (number > result) {
                max=number;
                System.out.println("范围缩小为（" + min + "," + max + ")");
            } else if (number < result) {
                min=number;
                System.out.println("范围缩小为（" + min + "," + max + ")");
            }
        }
    }
}


